// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config/config";
import func from "../config/func";
import AudioManage from "../manage/AudioManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Sound extends cc.Component {

    @property(cc.Node)
    m_soundBg: cc.Node = null;
    @property(cc.Slider)
    m_clickSlider: cc.Slider = null;
    @property(cc.Sprite)
    m_clickProgress: cc.Sprite = null;
    @property(cc.Sprite)
    m_clickHandle: cc.Sprite = null;
    @property(cc.Slider)
    m_bgSlider: cc.Slider = null;
    @property(cc.Sprite)
    m_bgProgress: cc.Sprite = null;
    @property(cc.Sprite)
    m_bgHandle: cc.Sprite = null;

    start(){
        // this.m_clickHandle.node.on(cc.Node.EventType.TOUCH_START, this.touchClickHandleStart, this);
        this.m_clickHandle.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchClickHandleCancel, this);
        this.m_clickHandle.node.on(cc.Node.EventType.TOUCH_END, this.touchClickHandleEnd, this);
        // this.m_bgHandle.node.on(cc.Node.EventType.TOUCH_START, this.touchBgHandleStart, this);
        this.m_bgHandle.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchBgHandleCancel, this);

        this.m_bgHandle.node.on(cc.Node.EventType.TOUCH_END, this.touchBgHandleEnd, this);
    }

    init() {
        config.openPopup = false;
        func.setNodeAni(this.m_soundBg);
        this.m_clickSlider.progress = config.clickSound;
        this.m_clickProgress.fillRange = config.clickSound;
        this.m_bgSlider.progress = config.bgSound;
        this.m_bgProgress.fillRange = config.bgSound;
    }
    /**
     * 设置点击音量
     */
    cliclSoundEvent() {
        config.clickSound = this.m_clickSlider.progress
        this.m_clickProgress.fillRange = config.clickSound;
    }

    touchClickHandleCancel(){
        AudioManage.audioManage.setEffectVolume();
    }
    touchClickHandleEnd(){
        AudioManage.audioManage.setEffectVolume();
    }
    /**
     * 设置背景音量
     */
    bgSoundEvent() {
        config.bgSound = this.m_bgSlider.progress;
        this.m_bgProgress.fillRange = config.bgSound;
    }
    touchBgHandleCancel(){
        AudioManage.audioManage.setBgAudioVolume();
    }
    touchBgHandleEnd(){
        AudioManage.audioManage.setBgAudioVolume();
    }
    close() {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        func.goReduceOut(this.m_soundBg, () => {
            this.node.active = false;
        })
    }
}
